﻿using NDF.Data.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UA.Data.Model;

namespace UA.Data
{
    public partial class UserAccessor
    {
        public bool ValidateCredential(string userCode, string password)
        {
            return userCode == null ? false : this.QuerySingle(u => u.Code == userCode && u.Password == password) != null;
        }

        public User GetByCode(string userCode)
        {
            return userCode == null ? null : this.QuerySingle(u => u.Code == userCode);
        }

        public User GetByID(int userID)
        {
            return this.QuerySingle(u => u.ID == userID);
        }

        public User GetByKey(Guid userKey)
        {
            return this.QuerySingle(u => u.Key == userKey);
        }




        public Power[] GetPowers(int userID)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var powers = from power in ctx.Set<Power>()
                             join rolePower in ctx.Set<RolePower>() on power.Key equals rolePower.PowerKey
                             join role in ctx.Set<Role>() on rolePower.RoleKey equals role.Key
                             join userRole in ctx.Set<UserRole>() on role.Key equals userRole.RoleKey
                             join user in ctx.Set<User>() on userRole.UserKey equals user.Key
                             where user.ID == userID
                             select power;
                return powers.ToArray();
            }
        }

        public Power[] GetPowers(Guid userKey)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var powers = from power in ctx.Set<Power>()
                             join rolePower in ctx.Set<RolePower>() on power.Key equals rolePower.PowerKey
                             join role in ctx.Set<Role>() on rolePower.RoleKey equals role.Key
                             join userRole in ctx.Set<UserRole>() on role.Key equals userRole.RoleKey
                             join user in ctx.Set<User>() on userRole.UserKey equals user.Key
                             where user.Key == userKey
                             select power;
                return powers.ToArray();
            }
        }

        public Power[] GetPowers(string userCode)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var powers = from power in ctx.Set<Power>()
                             join rolePower in ctx.Set<RolePower>() on power.Key equals rolePower.PowerKey
                             join role in ctx.Set<Role>() on rolePower.RoleKey equals role.Key
                             join userRole in ctx.Set<UserRole>() on role.Key equals userRole.RoleKey
                             join user in ctx.Set<User>() on userRole.UserKey equals user.Key
                             where user.Code == userCode
                             select power;
                return powers.ToArray();
            }
        }




    }
}
